package cn.lg.soar.system.api.model;

import cn.lg.soar.common.util.data.DataUtil;
import cn.lg.soar.core.dto.PermitLevel;
import cn.lg.soar.core.enums.PermitLevelEnum;
import cn.lg.soar.core.model.AccessTokenProps;

import java.util.Collections;
import java.util.Map;
import java.util.Set;

/**
 * @author luguoxiang
 * @date 2022/4/4
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
public class AuthProps extends AccessTokenProps {

    /**
     * 根据用户类型鉴权
     */
    private Map<PermitLevelEnum, Set<String>> permitLevel = Collections.emptyMap();

    /**
     * 根据用户类型鉴权
     */
    private Map<Integer, Set<String>> permitUserType = Collections.emptyMap();

    /**
     * 默认权限映射，无法判定具体权限级别时，默认为该级别（未配置 permitUserTypeDefault 时有效）
     */
    private PermitLevelEnum permitLevelDefault;

    /**
     * 默认权限映射，无法判定具体权限级别时，默认为该级别（优先级高于 permitLevelDefault）
     */
    private Set<Integer> permitUserTypeDefault;

    public Map<PermitLevelEnum, Set<String>> getPermitLevel() {
        return permitLevel;
    }

    public void setPermitLevel(Map<PermitLevelEnum, Set<String>> permitLevel) {
        this.permitLevel = permitLevel;
    }

    public Map<Integer, Set<String>> getPermitUserType() {
        return permitUserType;
    }

    public void setPermitUserType(Map<Integer, Set<String>> permitUserType) {
        this.permitUserType = permitUserType;
    }

    public PermitLevelEnum getPermitLevelDefault() {
        return permitLevelDefault;
    }

    public void setPermitLevelDefault(PermitLevelEnum permitLevelDefault) {
        this.permitLevelDefault = permitLevelDefault;
    }

    public Set<Integer> getPermitUserTypeDefault() {
        return permitUserTypeDefault;
    }

    public void setPermitUserTypeDefault(Set<Integer> permitUserTypeDefault) {
        this.permitUserTypeDefault = permitUserTypeDefault;
    }

    /**
     * 获取默认权限级别
     * @return
     */
    public PermitLevel getDefault() {
        if (DataUtil.isValuable(permitUserTypeDefault)) {
            return PermitLevel.valueOf(permitUserTypeDefault);
        }
        if (permitLevelDefault == null || PermitLevelEnum.user_type.equals(permitLevelDefault)) {
            return PermitLevel.authorized;
        }
        return PermitLevel.valueOf(permitLevelDefault);
    }

}
